/*
 * TaikaiApplication.java
 */

package taikai;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class TaikaiApplication extends SingleFrameApplication {

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        final DesktopApplication2View desktopApplication2View = new DesktopApplication2View(this);

        loadData(desktopApplication2View);

        show(desktopApplication2View);
    }

    private void loadData(final DesktopApplication2View desktopApplication2View) {
        final File file  = new File("default.ktd");

        try {

            final TaikaiFile taikaiFile = new TaikaiFile();
            taikaiFile.load(new FileReader(file));
            desktopApplication2View.setTeamEvent(taikaiFile.teamEvent);
            desktopApplication2View.data.getPlayers().load(taikaiFile);
            desktopApplication2View.stopWatch.setDuration(taikaiFile.duration);
            desktopApplication2View.data.setTeamEvent(taikaiFile.teamEvent);
        } catch (ParseException ex) {
            Logger.getLogger(TaikaiApplication.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TaikaiApplication.class.getName()).log(Level.SEVERE, file.getAbsolutePath(), ex);
        }
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of TaikaiApplication
     */
    public static TaikaiApplication getApplication() {
        return Application.getInstance(TaikaiApplication.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(TaikaiApplication.class, args);
    }
}
